import request from '@/config/axios'
import * as classifyApi from '@/api/classify'
import { assignWith } from 'lodash-es'
const router = useRouter() // 路由

export interface RoleVO {
  id: number
  name: string
  code: string
  sort: number
  status: number
  type: number
  dataScope: number
  dataScopeDeptIds: number[]
  createTime: Date
}

export interface UpdateStatusReqVO {
  id: number
  status: number
}

//查询元数据列表
export const list = async (params: PageParam) => {
  return await request.get({ url: '/meta/page', params })
}

// 查询角色列表
export const page = async (params: PageParam) => {
  return await request.get({ url: '/meta/page', params })
}

/**
 * 快捷创建元数据
 * @param params
 */
export const easyCreateMeta = async (params: PageParam) => {
  return await request.post({ url: '/meta/easy/create', data: params })
}

export const easyUpdateMeta = async (params: PageParam) => {
  return await request.post({ url: '/meta/easy/update', data: params })
}

export const selectOne = async (id: number) => {
  return await request.get({ url: '/meta/' + id + '/one' })
}

export const selectFieldOne = async (id) => {
  return await request.get({ url: '/meta/field/one/' + id })
}

export const deleteField = async (id: number) => {
  return await request.post({ url: '/meta/field/delete/' + id })
}

export const addField = async (params: PageParam, id: number) => {
  return await request.post({ url: '/meta/field/add/' + id, data: params })
}

export const updateField = async (params: PageParam, id: number) => {
  return await request.post({ url: '/meta/field/update/' + id, data: params })
}

export const selectField = async (params: PageParam, id: number) => {
  return await request.get({ url: '/meta/field/list/' + id, params })
}

export const selectMetaData = async (params: PageParam) => {
  return await request.get({ url: '/meta/data/42/list', params })
}

export const selectData = async (id, params: PageParam) => {
  return await request.get({ url: '/meta/data/' + id + '/list', params })
}

export const selectForm = async (id) => {
  return await request.get({ url: '/meta/one/' + id })
}

export const selectMetaDataOne = async (metaId, id) => {
  return await request.get({ url: '/meta/data/' + metaId + '/one/' + id })
}

export const selectMetaDataSourceOne = async (metaId, id) => {
  return await request.get({ url: '/meta/data/' + metaId + '/one/source/' + id })
}

export const selectFieldType = async () => {
  return await request.get({
    url: '/meta/field/getType'
  })
}

export const getMetaOne = async (id) => {
  return await request.get({
    url: '/meta/one/' + id
  })
}

export const updateMetaData = async (metaId, id, params) => {
  return await request.post({ url: '/meta/data/' + metaId + '/update/' + id, data: params })
}

export const createData = async (id, data) => {
  return await request.post({ url: '/meta/data/' + id + '/create', data: data })
}

/**
 * 转换VForm表单提交时的对象为Json字符串
 * @param param
 */
export const buildVFormData = (param: object) => {
  const keys = Object.keys(param)
  keys.forEach((key) => {
    let value = param[key]
    if (value) {
      if (typeof value == 'object') {
        value = JSON.stringify(value)
        param[key] = value
      }
    }
  })
  return param
}

//查询图书字段分类
export const getBookFieldClassify = async () => {
  return await classifyApi.selectClassify('bookField')
}
//添加资源
export const addResource = async (dataId, fieldId, data) => {
  return await request.post({
    url: '/meta/data/add/resource/' + dataId + '/' + fieldId,
    data: data
  })
}
//添加图片资源
export const addPicture = async (dataId, fieldId, data) => {
  return await request.post({
    url: '/meta/data/add/picture/' + dataId + '/' + fieldId,
    data: data
  })
}
//删除图片资源
export const removePicture = async (dataId, fieldId, id) => {
  return await request.post({
    url: '/meta/data/remove/picture/' + dataId + '/' + fieldId + '/' + id
  })
}
//添加视频资源
export const addVideo = async (dataId, fieldId, data) => {
  return await request.post({
    url: '/meta/data/add/video/' + dataId + '/' + fieldId,
    data: data
  })
}
//添加音频资源
export const addAudio = async (dataId, fieldId, data) => {
  return await request.post({
    url: '/meta/data/add/audio/' + dataId + '/' + fieldId,
    data: data
  })
}
export const selectFileField = async (id) => {
  return await request.get({ url: '/meta/field/' + id + '/selectFile/' })
}
export const selectFile = async (metaId, dataId, filed) => {
  return await request.get({ url: '/meta/file/' + metaId + '/' + dataId + '/' + filed + '/list' })
}

export const selectFieldValue = async (dataId, fieldId) => {
  const params = {
    dataId: dataId,
    fieldId: fieldId
  }
  return await request.get({ url: '/meta/data/select/field/value', params })
}

export const selectFieldVersionValue = async (dataId, fieldId, versionId) => {
  const params = {
    dataId: dataId,
    fieldId: fieldId,
    versionId: versionId
  }
  return await request.get({ url: '/meta/data/select/field/version/value', params })
}

export const download = async (metaId, ids) => {
  const params = {
    ids: ids
  }
  return await request.download({ url: '/meta/data/' + metaId + '/download', params })
}

export const deleted = async (metaId, ids) => {
  return await request.delete({ url: '/meta/data/' + metaId + '/delete', data: ids })
}
//元数据菜单路由
export const menu = async () => {
  return await request.get({ url: '/meta/menu' })
}
//审核资源
export const release = async (params) => {
  return await request.post({ url: '/meta/data/release', data: params })
}
//审核驳回资源
export const back = async (params) => {
  return await request.post({ url: '/meta/data/back', data: params })
}
//送审资源
export const auditing = async (parmas) => {
  return await request.post({ url: '/meta/data/auditing', data: parmas })
}
//取消发布资源
export const unRelease = async (metaId, ids) => {
  return await request.post({ url: '/meta/data/' + metaId + '/un/release', data: ids })
}
//签发资源
export const issue = async (metaId, ids) => {
  return await request.post({ url: '/meta/data/' + metaId + '/issue', data: ids })
}

export const revert = async (metaId, ids) => {
  return await request.post({ url: '/meta/data/' + metaId + '/revert', data: ids })
}

export const downloadById = async (metaId, id) => {
  return await request.download({ url: '/meta/data/' + metaId + '/download/' + id })
}
export const exportByIds = async (metaId, ids) => {
  const params = {
    ids: ids.join(';')
  }
  return await request.download({ url: '/meta/data/' + metaId + '/export', params })
}
export const exportTemplate = async (metaId) => {
  return await request.download({url: '/meta/data/' + metaId + '/exportTemplate'})
};

export const uploadMeta = async (metaId, file) => {
  return await request.upload({ url: '/meta/data/' + metaId + '/upload', data: file })
}
export const uploadMetaMerge = async (metaId, uuid, chunkTotal, size, ext) => {
  return await request.get({
    url:
      '/meta/data/' + metaId + '/upload?uuid=' +
      uuid +
      '&chunkTotal=' +
      chunkTotal +
      '&fileSize=' +
      size +
      '&ext=' +
      ext
  })
}
export const uploadChunk = async (metaId, data) => {
  return await request.upload({ url: '/meta/data/' + metaId + '/uploadChunk', data: data })
}

export const generatePDFReport = async (reportInfo) => {
  return await request.post({ url: '/meta/data/generatePDFReport', data: reportInfo })
}

//删除回收站资源
export const deleteMetaDataByIds = async (metaId, ids) => {
  return await request.delete({ url: '/meta/data/' + metaId + '/deleteMetaDataByIds', data: ids })
}
// 新增资源副本
export const addCopy = async (metaId, ids) => {
  return await request.post({ url: '/meta/data/' + metaId + '/copy', data: ids })
}
// 检查资源标题是否重复
export const checkOverlap = async (metaId, parmas) => {
  return await request.post({ url: '/meta/data/' + metaId + '/check', data: parmas })
}

//查询元数据数据版本记录
export const metaVersionList = async (params: PageParam) => {
  return await request.get({ url: '/meta/data/version', params })
}

//删除元数据数据版本记录
export const metaVersionDelete = async (ids) => {
  return await request.delete({ url: '/meta/data/version/delete', data:ids })
}

//查询单条元数据数据版本记录
export const getMetaVersion = async (id) => {
  return await request.get({ url: '/meta/data/version/'+ id })
}

//获取空间数据类型字典表
export const selectDataType = async () => {
  return await request.get({
    url: '/data/dicCustommanager/getDataType'
  })
}
